package com.alex.boot.controller;

import com.alex.boot.config.BaseException;
import com.alex.boot.config.RabbitMQConfig;
import com.alex.boot.dao.primary.PrimaryMapper;
import com.alex.boot.dao.secondary.SecondaryMapper;
import com.alex.boot.entity.MailEntity;
import com.alex.boot.service.RedisService;
import com.alex.starter.service.CustomerService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Random;

/**
 * 测试拦截器
 */
@RestController
@RequestMapping(value = "/test")
public class TestController {

    @Autowired
    private CustomerService customerService;

    @Autowired
    private RedisService redisService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired(required = false)
    private PrimaryMapper primaryMapper;

    @Autowired
    private SecondaryMapper secondaryMapper;

    @GetMapping(value = "/interceptor")
    public String testInterceptor() {
        return "Hello interceptor!";
    }

    @GetMapping(value = "/starter")
    public String starter() {
        return customerService.findCustomer();
    }

    @GetMapping(value = "/redis")
    public String redis() {
        return redisService.findOrderDetail("123");
    }

    @GetMapping(value = "/default/mq")
    public void defaultMq() {
        MailEntity mailEntity = MailEntity.builder()
                .id(new Random().nextLong())
                .data("测试发送消息体----默认ACK")
                .createTime(new Date())
                .build();
//        MailEntity mailEntity = new MailEntity();
        rabbitTemplate.convertAndSend(RabbitMQConfig.DEFAULT_MAIL_QUEUE, mailEntity);
    }

    @GetMapping(value = "/manual/mq")
    public void manualMq() {
        MailEntity mailEntity = MailEntity.builder()
                .id(new Random().nextLong())
                .data("测试发送消息体----手动ACK")
                .createTime(new Date())
                .build();
//        MailEntity mailEntity = new MailEntity();
        rabbitTemplate.convertAndSend(RabbitMQConfig.MANUAL_MAIL_QUEUE, mailEntity);
    }

    @PostMapping(value = "/primary")
    public void usePrimary() {
        MailEntity mailEntity = MailEntity.builder()
                .id(new Random().nextLong())
                .data("Primary DB")
                .createTime(new Date())
                .build();

        primaryMapper.insertSelective(mailEntity);
    }

    @PostMapping(value = "/secondary")
    public void useSecondary() {
        MailEntity mailEntity = MailEntity.builder()
                .id(new Random().nextLong())
                .data("secondary DB")
                .createTime(new Date())
                .build();

        secondaryMapper.insertSelective(mailEntity);

        System.out.println("first: " + mailEntity.getId());

        mailEntity.setId(new Random().nextLong());
        System.out.println("second: " + mailEntity.getId());
        secondaryMapper.insertSelectiveIsMe(mailEntity);
    }

    /**
     * 关于@ModelAttribute,
     * 可以使用ModelMap以及@ModelAttribute()来获取参数值。
     */
    @GetMapping("/one")
    public String testError(ModelMap modelMap ) {
        throw new BaseException("系统发生500异常！" + modelMap.get("attribute"));
    }

    @GetMapping("/two")
    public String testTwo(@ModelAttribute("attribute") String attribute) {
        throw new BaseException("系统发生500异常！" + attribute);
    }

    @GetMapping("/date")
    public void testDate1(@RequestParam("date") Date date) {
        System.out.println(date);
    }

    @PostMapping("/date2")
    public void testDate2(@RequestBody MailEntity mailEntity) {
        System.out.println(mailEntity);
    }


}
